package cn.aicnn.chatssespringboot.controller;

import cn.aicnn.chatssespringboot.common.CommonResponse;
import cn.aicnn.chatssespringboot.dto.Account;
import cn.aicnn.chatssespringboot.dto.IfIpRequest;
import cn.aicnn.chatssespringboot.dto.IpRequest;
import cn.aicnn.chatssespringboot.dto.User;
import cn.aicnn.chatssespringboot.persistence.accountMapper;
import cn.aicnn.chatssespringboot.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/api/users/register")
    public Map<String, String> register(@RequestBody Account account) {
        Map<String, String> response = new HashMap<>();

        boolean success = accountService.register(account);
        if (success) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
            response.put("data", "注册成功");
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
            response.put("data", "用户名已存在或注册失败");
        }

        return response;
    }

    @GetMapping("/api/users/{username}")

    public CommonResponse<Account> getUserInfo(@PathVariable String username) {
//        Map<String, Object> response = new HashMap<>();

//        Account user = accountService.getUserInfo(username);
//        if (user != null) {
//            response.put("code", "0");
//            response.put("message", "SUCCESS");
//            response.put("data", user.getAddress());
//        } else {
//            response.put("code", "1");
//            response.put("message", "ERROR");
//            response.put("data" , "");
//        }
        Account user = accountService.getUserInfo(username);
        return CommonResponse.createForSuccess("SUCCESS", user);

    }

    @PutMapping("/api/users/updateInfo/{username}")
    public Map<String, String> updateInfo(@PathVariable String username, @RequestBody Account newInfo) {
        Map<String, String> response = new HashMap<>();

        boolean success = accountService.updateUserInfo(username, newInfo);
        if (success) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
            response.put("data", "更新用户信息成功");
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
            response.put("data", "更新用户信息失败");
        }

        return response;
    }


    @GetMapping("/api/users/getquestion/{username}")
    public Map<String, Object> getSecurityQuestion(@PathVariable String username) {
        Map<String, Object> response = new HashMap<>();

        String question = accountService.getSecurityQuestion(username);
        if (question != null) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
            response.put("data", question);
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
            response.put("data", "");
        }

        return response;
    }

    @PostMapping("/api/users/verifyAnswer/{username}")
    public Map<String, String> verifyAnswer(@PathVariable String username, @RequestParam String answer) {
        Map<String, String> response = new HashMap<>();

        boolean success = accountService.verifyAnswer(username, answer);
        if (success) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
        }

        return response;
    }

    @PostMapping("/api/users/updatePwd/{username}")
    public Map<String, String> updatePassword(@PathVariable String username, @RequestBody Account request) {
        Map<String, String> response = new HashMap<>();

        boolean success = accountService.updatePassword(username, request.getPassword() , request.getAnswer());
        if (success) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
        }

        return response;
    }

    @GetMapping("/api/users/updateInfo/{username}")
    public Map<String, Object> getAdmin(@PathVariable String username) {
        Map<String, Object> response = new HashMap<>();
        boolean isAdmin = accountService.isAdmin(username);

        if (isAdmin) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
            response.put("data", 1); // 1 表示ip可用
        } else {
            response.put("code", "1");
            response.put("message", "ERROR");
            response.put("data", 0); // 0 表示ip不可用
        }

        return response;
    }

    @GetMapping("/api/users/isExist")
    public Map<String, Object> isExist(@RequestParam String username) {
        Map<String, Object> response = new HashMap<>();
        boolean userExists = accountService.checkUserExist(username);

        if (userExists) {
            response.put("code", "0");
            response.put("message", "SUCCESS");
            response.put("data", "exist"); // 用户存在
        } else {
            response.put("code", "0");
            response.put("message", "ERROR");
            response.put("data", "no exist"); // 用户不存在
        }

        return response;
    }

    @PostMapping("/api/user/{username}/ip")
    public ResponseEntity<String> updateAddressWithIp(@PathVariable String username, @RequestBody IpRequest request) {
        int rowsAffected = accountService.updateAddressByUsername(username, request.getIp());
        if (rowsAffected > 0) {
            return ResponseEntity.ok("IP地址已成功更新到用户的地址中");
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping("/api/user/{username}/ifip")
    public Map<String, Object> setAdminValue(@PathVariable String username, @RequestBody IfIpRequest requestBody) {
        Map<String, Object> response = new HashMap<>();
        System.out.println("ifip:dasd"+ requestBody.getIfip());
                boolean success = accountService.setAdmin(username, requestBody.getIfip());
                if (success) {
                    response.put("code", "0");
                    response.put("message", "SUCCESS");
                } else {
                    response.put("code", "1");
                    response.put("message", "ERROR");
                }
        return response;
    }
}
